package Controller;

import Beans.Student;
import Service.StudentService;
import Utils.MSG;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.xml.ws.Binding;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName StudentController
 * @Description TODO
 * @Author Administrator
 * @Date 2021/7/415:49
 * @Version 1.0
 **/
@Controller
public class StudentController {
    @Autowired
    StudentService studentService;

    //接收进入student页面请求，
    //进行分页并返回Json字符串
    @RequestMapping("/students")
    @ResponseBody//将java对象包装成json格式
    public MSG getStudentWithJson(@RequestParam(value = "page", defaultValue = "1") Integer page) {
        PageHelper.startPage(page, 5);
        List<Student> students = studentService.getAll();
        PageInfo<Student> pageInfo = new PageInfo<>(students, 3);
        return MSG.success().add("pageInfo", pageInfo);
    }

    //根据id返回学生名
    @RequestMapping(value = "/getName", method = RequestMethod.GET)
    @ResponseBody//将java对象包装成json格式
    public MSG getName(@RequestParam(value = "id") Integer id) {
        String name = studentService.selectName(id);
        return MSG.success().add("name", name);

    }

    //接收数据并转换成student对象存入数据库
    @RequestMapping(value = "/student", method = RequestMethod.POST)
    @ResponseBody//将java对象包装成json格式
    public MSG setStudentWithJson(Student student, BindingResult result) {
        //后端校验
        if (result.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError error : errors) {
                System.out.println("错误字段：" + error.getField());
                System.out.println("错误信息：" + error.getDefaultMessage());
                map.put(error.getField(), error.getDefaultMessage());
            }
            return MSG.fail().add("errorFields", map);
        } else {
            studentService.insert(student);
            return MSG.success();
        }


    }

    //从数据库中删除student对象
    @RequestMapping(value = "/student/{id}", method = RequestMethod.DELETE)
    @ResponseBody//将java对象包装成json格式
    public MSG deleteStudentWithJson(@PathVariable("id") Integer id) {
        studentService.delete(id);
        return MSG.success();
    }


    //更新student对象存入数据库
    @RequestMapping(value = "/student/{studentId}", method = RequestMethod.PUT)
    @ResponseBody//将java对象包装成json格式
    public MSG putStudentWithJson(Student student) {
        studentService.update(student);
        return MSG.success();
    }

    //从数据库中删除student对象
    @RequestMapping(value = "/student/deleteCheck/{ids}", method = RequestMethod.DELETE)
    @ResponseBody//将java对象包装成json格式
    public MSG deleteStudentWithJson(@PathVariable("ids") String ids) {
        String idsr = "";
        for (int i = 9; i < ids.length(); i++) {
            idsr += ids.charAt(i);
        }
        List<String> idsArr = new ArrayList<>();
        String id = "";
        for (int i = 0; i < idsr.length(); i++) {
            if (idsr.charAt(i) != '-') {
                id += idsr.charAt(i);
            } else {
                idsArr.add(id);
                id = "";
            }
        }
        for (int i = 0; i < idsArr.size(); i++) {
            studentService.delete(Integer.parseInt(idsArr.get(i)));
        }
        return MSG.success();
    }


}










